/*
   @Copyright:LeetCode
   @Author:   tjyemail
   @Problem:  http://leetcode.com/problems/remove-nth-node-from-end-of-list
   @Language: C++
   @Datetime: 19-05-30 13:21
   */

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
	ListNode* removeNthFromEnd(ListNode* head, int n) {
		if(n==0 || head==NULL) return head;
		ListNode H(-1), *s=&H, *f;
		for(f=head; n--; f=f->next);
		for(H.next=head; f; s=s->next, f=f->next);
		f=s->next;
		s->next=f->next;
		delete f;
		return H.next;
	}
};
